package com.ruoyi.sale.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.ProductCates;
import com.ruoyi.common.core.domain.ShipmentStatus;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.mapper.ShipmentStatusMapper;
import com.ruoyi.sale.service.IShipmentStatusService;

/**
 * 计划状态Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-13
 */
@Service
public class ShipmentStatusServiceImpl implements IShipmentStatusService 
{
    @Autowired
    private ShipmentStatusMapper shipmentStatusMapper;

    /**
     * 查询计划状态
     * 
     * @param id 计划状态主键
     * @return 计划状态
     */
    @Override
    public ShipmentStatus selectShipmentStatusById(Long id)
    {
        return shipmentStatusMapper.selectShipmentStatusById(id);
    }

    /**
     * 查询计划状态列表
     * 
     * @param shipmentStatus 计划状态
     * @return 计划状态
     */
    @Override
    @DataScope(ShipmentStatusAlias = "s")
    public List<ShipmentStatus> selectShipmentStatusList(ShipmentStatus shipmentStatus)
    {
        return shipmentStatusMapper.selectShipmentStatusList(shipmentStatus);
    }

    @Override
    public List<TreeSelect> selectShipmentStatusTreeList(ShipmentStatus shipmentStatus) {
        List<ShipmentStatus> shipmentStatusList = SpringUtils.getAopProxy(this).selectShipmentStatusList(shipmentStatus);
        return buildShipmentStatusTreeSelect(shipmentStatusList);
    }

    @Override
    public List<ShipmentStatus> buildShipmentStatusTree(List<ShipmentStatus> shipmentStatusList) {
        List<ShipmentStatus> returnList = new ArrayList<>();
        List<Long> collect = shipmentStatusList.stream().map(ShipmentStatus::getId).collect(Collectors.toList());
        for (ShipmentStatus shipmentStatus : shipmentStatusList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(shipmentStatus.getParentId())){
                recursionFn(shipmentStatusList,shipmentStatus);
                returnList.add(shipmentStatus);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = shipmentStatusList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildShipmentStatusTreeSelect(List<ShipmentStatus> shipmentStatusList) {
        List<ShipmentStatus> shipmentStatusTrees = buildShipmentStatusTree(shipmentStatusList);
        return shipmentStatusTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     * @param shipmentStatusList 子级
     * @param shipmentStatus 父级
     */
    private void recursionFn(List<ShipmentStatus> shipmentStatusList , ShipmentStatus shipmentStatus){
        // 得到子节点列表
        List<ShipmentStatus> childList = getChildList(shipmentStatusList, shipmentStatus);
        shipmentStatus.setChildrenShipmentStatus(childList);
        for (ShipmentStatus newStatus : childList)
        {
            if (hasChild(shipmentStatusList, newStatus))
            {
                recursionFn(shipmentStatusList, newStatus);
            }
        }
    }

    /**
     * 获得子节点
     * @param shipmentStatusList
     * @param shipmentStatus
     * @return
     */
    private List<ShipmentStatus> getChildList(List<ShipmentStatus> shipmentStatusList, ShipmentStatus shipmentStatus){
        List<ShipmentStatus> returnList = new ArrayList<>();
        Iterator<ShipmentStatus> iterator = shipmentStatusList.iterator();
        while (iterator.hasNext()){
            ShipmentStatus n = (ShipmentStatus) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == shipmentStatus.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ShipmentStatus> shipmentStatusList, ShipmentStatus shipmentStatus)
    {
        return getChildList(shipmentStatusList, shipmentStatus).size() > 0;
    }


}
